import {
  ErrorRequestHandler,
  Request,
  Response,
  response,
  NextFunction,
} from "express";
import fs from "fs";
import path from "path";
import url from "url";
import { img } from "../config";
import { err, info } from "../router/middleware/infoerr";
import ImgSer from "../services/ImgSer";

export async function untheft(req: Request, res: Response, next: NextFunction) {
  if (!req.headers.referer) return next();
  const referer = url.parse(req.headers.referer).host;
  if (req.headers.host === referer) return next();
  if (path.extname(req.path)) {
    const ext = path.extname(req.path);
    if (img.includes(ext)) {
      const result = await ImgSer.findfilename(req.path);
      console.log(result);
      if (!result) return next();
      req.url = "/img/空.jpg";
    }
  }
  next();
}
